package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.constant.DBConstant;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import com.sec.etech.sys.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.form.util.DateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IsmGetWelderExaminationRecordImpl implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;

    public IsmGetWelderExaminationRecordImpl() {
    }

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {
        JSONObject result = new JSONObject();
        this.checkAndGetData(businessParams);
        List<JSONObject> resList = this.getWelderExaminationRecord(businessParams.getJSONObject(SignatureConstant.INPUT_PARAMS));
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null,
                businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", resList);
        return result;
    }

    private void checkAndGetData(JSONObject busParams) throws BusinessMessage {
        JSONObject object = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS);
        if (object.get("codeList") == null || object.getJSONArray("codeList").size() == 0) {
            throw new BusinessMessage("请求中缺少参数，请检查！");
        }
    }

    private List<JSONObject> getWelderExaminationRecord(JSONObject object) throws Exception {

        JdbcTemplate slaveJdbcTemplate = DbUtil.getJdbcTemplate(DBConstant.ETECH_SLAVE);
        List<String> codeList = (List<String>) object.get("codeList");
        //查询组装数据
        List<JSONObject> resList = new ArrayList<>();
        for (String code : codeList) {
            String sql = "select t2.qualification_code,t3.status_name,t4.method,t1.effective_date,t5.fullname,t5.fullname_ " +
                    "from welder_examination_record t1 " +
                    " join welder_examination_record_z_qualification_code t2 on t1.qualification_code=t2.id " +
                    " left join welder_examination_record_z_status t3 on t1.`status`=t3.id " +
                    " left join weld_procedure_z_weld_method t4 on t1.weld_method=t4.id " +
                    " left join " +
                    "  (select a.id_,a.fullname_,c.fullname from org_user a, org_relation b,org_group_fullname c " +
                    "  where a.id_=b.user_id_ and b.group_id_=c.group_id) t5 on t1.welder = t5.id_ " +
                    "where t2.qualification_code=?";
            List<Map<String, Object>> recordList = slaveJdbcTemplate.queryForList(sql, code);
            if (recordList == null || recordList.size() == 0) {
                continue;
            }
            JSONObject obj = new JSONObject();
            obj.put("qualificationCode", recordList.get(0).get("qualification_code"));//资格代号
            obj.put("status", recordList.get(0).get("status_name"));//资格状态
            obj.put("weldMethod", recordList.get(0).get("method"));//焊接方法
            obj.put("welder", recordList.get(0).get("fullname_"));//焊工
            obj.put("deptName", recordList.get(0).get("fullname"));//部门
            if (recordList.get(0).get("effective_date") != null) {//资格生效日期
                obj.put("effectiveDate", DateUtils.format((Date) recordList.get(0).get("effective_date"), "yyyy-MM-dd"));
            } else {
                obj.put("effectiveDate", "");
            }
            resList.add(obj);
        }
        return resList;
    }

    @Override
    public String methodName() {
        return "getWelderExaminationRecord";
    }
}
